package tw.rescue.struts.action;

import tw.rescue.model.slim3.model.Account;

public class UserTableAction {

	private Account user;
	private java.util.Map<String, String> errorMessage = new java.util.HashMap<String, String>();
	
	public void setUser(Account user) {
		this.user = user;
	}
	
	public Account getUser() {
		return user;
	}
	
	public void setErrorMessage(java.util.Map<String, String> errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	public java.util.Map<String, String> getErrorMessage() {
		return errorMessage;
	}

	/*
	 * 可以不用在這邊做驗證，只是...要找資料看struts2的驗證如何和json搭配。
	 */
	
	public String register() throws Exception {
		if (user.getEmail() == null || "".equals(user.getEmail().trim())) {
			errorMessage.put("email", "Please enter email.");
		}
		
		if (user.getPassword() == null || "".equals(user.getPassword().trim())) {
			errorMessage.put("password", "Please enter password.");
		}
		
		if (user.getNickname() == null || "".equals(user.getNickname().trim())) {
			errorMessage.put("username", "Please enter Nickname.");
		}

		// TODO 為了編繹通過，暫時 comment
//		if (user.getPhone() == null || "".equals(user.getPhone().trim())) {
//			errorMessage.put("phone", "Please enter phone.");
//		}
		
		if (!errorMessage.isEmpty()) {
			return "error";
		}
		
		return "success";
	}
	
	public String login() throws Exception {
		if (user.getEmail() == null || "".equals(user.getEmail().trim())) {
			errorMessage.put("email", "Please enter email.");
		}
		
		if (user.getPassword() == null || "".equals(user.getPassword().trim())) {
			errorMessage.put("password", "Please enter password.");
		}
		
		if (!errorMessage.isEmpty()) {
			return "error";
		}
		
		if ("test@gmail.com".equals(user.getEmail())) {
			if ("1234".equals(user.getPassword())) {
				return "success";
			}
		} else {
			errorMessage.put("user", "Login fail.");
		}
		
		return "error";
	}
}
